<template>
  <el-container class='elheight'>
    <el-aside class='appTree'>
      <app-tree :tree-data="treeData" v-on:appTreeClick="appTreeClick"></app-tree>
    </el-aside>
    <el-main>
      <el-container>
        <el-header class="dzy-main-header">
          <div class="btn-group-el">
            <btn-group :currentSelectedData="currentSelectedData" @btnClickCallback="btnClickCallback"></btn-group>
          </div>
          <div class="dzy-custom-search-box">
            <div>
              身份证号：<el-input placeholder="请输入内容" v-model="selectForm.identityNumber" clearable class="dzy-custom-input"></el-input>
            </div>
            <div>
              姓名：<el-input placeholder="请输入内容" v-model="selectForm.name" clearable class="dzy-custom-input"></el-input>
            </div>
            <div>
              性别：
              <el-select v-model="selectForm.gender" clearable placeholder="请选择" :disabled="disabled">
                <el-option v-for="(item,index) in genderOption" :key="index" :label="item.name" :value="item.code"></el-option>
              </el-select>
            </div>
            <div>
              联系方式：<el-input placeholder="请输入内容" v-model="selectForm.phone" clearable class="dzy-custom-input"></el-input>
            </div>
            <div>
              矫正类型：
              <el-select v-model="selectForm.correctionCategory" clearable filterable placeholder="请选择">
                <el-option v-for="(item,index) in correctionOption" :key="index" :label="item.name" :value="item.code"></el-option>
              </el-select>
            </div>
            <div>
              是否有托管：
              <el-select v-model="selectForm.takeOffStatus" clearable placeholder="请选择">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="0"></el-option>
              </el-select>
            </div>
            <div>
              是否有漏管：
              <el-select v-model="selectForm.missingManagementStatus" clearable placeholder="请选择">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="0"></el-option>
              </el-select>
            </div>
            <div>
              <el-button @click="searchHandleClick" type="primary" icon="el-icon-search">查询</el-button>
            </div>
          </div>
        </el-header>
        <el-main>
          <app-table
            :page-data="pageData"
            :table-columns="tableColumns"
            v-on:currentChange="handleCurrentChange"
            v-on:handleCheckedRows="handleCheckedRows">
          </app-table>
          <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" class="custom-dialog_style" :before-close="cancel" :close-on-click-modal="false">
            <el-form :model="formData" :rules="rules" :disabled="showOnly" ref="formData">
              <el-collapse v-model="activeName" accordion class="custom-collapse-style">
                <el-collapse-item title="户籍基本信息" name="1">
                  <el-form-item label="身份证号：" prop="identityNumber">
                    <el-input v-model="formData.identityNumber" autocomplete="off" @blur="identityNumberHaveDone(formData.identityNumber)" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="出生日期：" prop="birthday">
                    <el-date-picker
                      v-model="formData.birthday"
                      value-format="timestamp"
                      format="yyyy 年 MM 月 dd 日"
                      placeholder="选择日期时间"
                      :picker-options="dataPickerBeforeTodayOptions"
                      :readonly="isReadOnly">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="姓名：" prop="name">
                    <el-input v-model="formData.name" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="曾用名：">
                    <el-input v-model="formData.usedName" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="性别：" prop="gender">
                    <el-select v-model="formData.gender" clearable placeholder="请选择" :disabled="disabled">
                      <el-option v-for="(item,index) in genderOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="民族：" prop="nation">
                    <el-select v-model="formData.nation" clearable filterable placeholder="请选择" :disabled="disabled">
                      <el-option v-for="(item,index) in nationOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="籍贯：" prop="birthplace">
                    <el-input v-model="formData.birthplace" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="婚姻状况：" prop="maritalStatus">
                    <div class="block">
                      <el-cascader
                        v-model="formData.maritalStatusArr"
                        placeholder="请选择"
                        :options="maritalStatusOption"
                        filterable
                        :props="dictProps"
                        clearable
                        :show-all-levels="false"
                        :disabled="disabled">
                      </el-cascader>
                    </div>
                  </el-form-item>
                  <el-form-item label="政治面貌：">
                    <el-select v-model="formData.politicalStatus" clearable placeholder="请选择" filterable :disabled="disabled">
                      <el-option v-for="(item,index) in politicalStatusOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="学历：" prop="education">
                    <div class="block">
                      <el-cascader
                        v-model="formData.educationArr"
                        placeholder="请选择"
                        :options="educationOption"
                        filterable
                        :props="dictProps"
                        clearable
                        change-on-select
                        :disabled="disabled">
                      </el-cascader>
                    </div>
                  </el-form-item>
                  <el-form-item label="宗教信仰：">
                    <el-select v-model="formData.religion" clearable filterable placeholder="请选择" :disabled="disabled">
                      <el-option v-for="(item,index) in religionOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="职业类别：">
                    <el-select v-model="formData.careerCategory" clearable filterable placeholder="请选择" :disabled="disabled">
                      <el-option v-for="(item,index) in careerCategoryOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="职业：">
                    <el-input v-model="formData.career" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="服务处所：">
                    <el-input v-model="formData.servicePlace" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="联系方式：" prop="phone">
                    <el-input v-model="formData.phone" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="户籍地：" prop="householdAddr">
                    <el-input v-model="formData.householdAddr" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="户籍门(楼)详址：" prop="householdAddrDetail">
                    <el-input v-model="formData.householdAddrDetail" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="现住地：" prop="currentAddr">
                    <el-input v-model="formData.currentAddr" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                  <el-form-item label="现住门(楼)详址：" prop="currentAddrDetail">
                    <el-input v-model="formData.currentAddrDetail" autocomplete="off" :readonly="isReadOnly"></el-input>
                  </el-form-item>
                </el-collapse-item>
                <el-collapse-item title="其他信息" name="2">
                  <el-form-item label="社区矫正人员编号：">
                    <el-input v-model="formData.redressCode" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="原羁押场所：">
                    <el-input v-model="formData.firstCustodyPlace" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="矫正类别：">
                    <el-select v-model="formData.correctionCategory" clearable filterable placeholder="请选择">
                      <el-option v-for="(item,index) in correctionOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="案件类别：">
                    <el-select v-model="formData.caseType" clearable filterable placeholder="请选择">
                      <el-option v-for="(item,index) in caseTypeOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="具体罪名：">
                    <el-input v-model="formData.crimeName " autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="原判刑期：">
                    <el-input v-model="formData.sentencePeriod  " autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="原判刑开始日期：">
                    <el-date-picker
                      v-model="formData.sentenceStart"
                      value-format="timestamp"
                      format="yyyy 年 MM 月 dd 日"
                      placeholder="选择日期时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="原判刑结束日期：">
                    <el-date-picker
                      v-model="formData.sentenceEnd"
                      value-format="timestamp"
                      format="yyyy 年 MM 月 dd 日"
                      placeholder="选择日期时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="刑罚变更执行情况：">
                    <el-input v-model="formData.rewardsPunishmentsChange" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="矫正开始日期：">
                    <el-date-picker
                      v-model="formData.correctionStrat"
                      value-format="timestamp"
                      format="yyyy 年 MM 月 dd 日"
                      placeholder="选择日期时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="矫正结束日期：">
                    <el-date-picker
                      v-model="formData.correctionEnd"
                      value-format="timestamp"
                      format="yyyy 年 MM 月 dd 日"
                      placeholder="选择日期时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="接收方式：">
                    <el-select v-model="formData.acceptType" clearable filterable placeholder="请选择">
                      <el-option v-for="(item,index) in acceptTypeOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="四史情况：">
                    <el-select v-model="formData.fourHistoryArr" multiple placeholder="请选择">
                      <el-option v-for="(item,index) in fourHistoryOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="是否累惯犯：">
                    <el-select v-model="formData.recidivistStatus" clearable placeholder="请选择">
                      <el-option label="是" :value="1"></el-option>
                      <el-option label="否" :value="0"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="“三涉”情况：">
                    <el-select v-model="formData.threeRelatedArr" multiple placeholder="请选择">
                      <el-option v-for="(item,index) in threeRelatOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="是否建立矫正小组：">
                    <el-select v-model="formData.correctionTeam" clearable placeholder="请选择">
                      <el-option label="是" :value="1"></el-option>
                      <el-option label="否" :value="0"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="矫正小组人员组成情况：">
                    <el-select v-model="formData.correctionTeamArr" multiple placeholder="请选择">
                      <el-option v-for="(item,index) in correctionTeamOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="矫正解除（终止）类型：">
                    <el-select v-model="formData.releaseCorrectionType" clearable filterable placeholder="请选择">
                      <el-option v-for="(item,index) in releaseOption" :key="index" :label="item.name" :value="item.code"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="是否有脱管：">
                    <el-select v-model="formData.takeOffStatus" clearable placeholder="请选择">
                      <el-option label="是" :value="1"></el-option>
                      <el-option label="否" :value="0"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="脱管原因：">
                    <el-input v-model="formData.takeOffReason" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="检查监督脱管情况：">
                    <el-input v-model="formData.checkTakeOffSituation" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="脱管纠正情况：">
                    <el-input v-model="formData.takeOffCorrectSituation" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="是否有漏管：">
                    <el-select v-model="formData.missingManagementStatus" clearable placeholder="请选择">
                      <el-option label="是" :value="1"></el-option>
                      <el-option label="否" :value="0"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="漏管原因：">
                    <el-input v-model="formData.missingManagementReason" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="检查监督漏管情况：">
                    <el-input v-model="formData.checkMissingManagementSituation" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="漏管纠正情况：">
                    <el-input v-model="formData.correctMissingManagementSituation" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="奖惩情况：">
                    <el-input v-model="formData.rewardsPunishments" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="是否重新犯罪：">
                    <el-select v-model="formData.renewCrime" clearable placeholder="请选择">
                      <el-option label="是" :value="1"></el-option>
                      <el-option label="否" :value="0"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="重新犯罪罪名：">
                    <el-input v-model="formData.renewCrimeName" autocomplete="off"></el-input>
                  </el-form-item>
                </el-collapse-item>
                <el-collapse-item title="居住地信息" name="3">
                  <el-form-item label="街道/社区/网格：" prop="liveGridIdArr">
                    <el-cascader
                      placeholder="请选择"
                      :options="treeData"
                      filterable
                      :props="liveGridIdProps"
                      :show-all-levels="false"
                      clearable @change="afterGridIdCheck"
                      v-model="formData.liveGridIdArr">
                    </el-cascader>
                  </el-form-item>
                  <el-form-item label="居住地类型：">
                    <el-radio-group v-model="placeType" size="medium" @change="placeTypeChangedEvt">
                      <el-radio-button label="小区" ></el-radio-button>
                      <el-radio-button label="院落"></el-radio-button>
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item label="院落：" v-if="isYardSelectShow">
                    <el-select v-model="formData.courtyardId" clearable filterable placeholder="请选择">
                      <el-option v-for="(yard,index) in yardOptions" :key="index" :label="yard.name" :value="yard.id"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="小区：" v-if="isCommunitySelectShow">
                    <el-select v-model="formData.communityId" clearable filterable placeholder="请选择" @change="afterCommunityIdCheck">
                      <el-option v-for="(community,index) in communityOptions" :key="index" :label="community.name" :value="community.id"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="楼栋：" v-if="isCommunitySelectShow">
                    <el-select v-model="formData.buildingId" clearable filterable placeholder="请选择" @change="afterBuildIdCheck">
                      <el-option v-for="(build,index) in buildingOptions" :key="index" :label="build.buildingName" :value="build.id"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="单元：" v-if="isCommunitySelectShow">
                    <el-select v-model="formData.unitId" clearable filterable placeholder="请选择" @change="afterUnitIdCheck">
                      <el-option v-for="(unit,index) in unitOptions" :key="index" :label="unit.unitName" :value="unit.id"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="房屋：" v-if="isCommunitySelectShow">
                    <el-select v-model="formData.houseId" clearable filterable placeholder="请选择">
                      <el-option v-for="(house,index) in houseOptions" :key="index" :label="house.houseNumber" :value="house.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-collapse-item>
              </el-collapse>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="isfooterBtnShow">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="save('formData')">确 定</el-button>
            </div>
          </el-dialog>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
</template>

<script>
import AppTree from '@/components/common/AppTree'
import AppTable from '@/components/AppTable'
import BtnGroup from '@/components/BtnGroup'
import {getAllRegion} from '@/utils/region'
import {validateIdefer} from '@/utils/validate'
import {getPopulation} from '@/http/PopulationApi'
import {getDictDataOption} from '@/utils/getDictDataOption'
import {getUserGridTree, getUserInfo} from '@/utils/SessionDataUtil'
import {findCommunityList, findBuildingList, findUnitList, findHouseList, getHouse, findCourtyardList} from '@/http/FangWuGuanLiApi'
import {findCorrector, createCorrector, updateCorrector, delCorrector, getCorrector} from '@/http/SpecialCrowdApi'
export default {
  data () {
    return {
      activeName: '1',
      dialogFormVisible: false,
      isfooterBtnShow: true,
      specialPassword: false,
      treeData: [],
      pageData: {},
      dialogTitle: '',
      currentSelectedData: [],
      showOnly: false,
      tableColumns: [
        { prop: 'name', label: '姓名' },
        { prop: 'identityNumber', label: '证件号码' },
        { prop: 'career', label: '职业' },
        { prop: 'phone', label: '联系方式' },
        { prop: 'birthplace', label: '籍贯' },
        { prop: 'correctionCategory', label: '矫正类型', formatter: this.correctionCategoryFormatter }
      ],
      dataPickerBeforeTodayOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      tableDada: [],
      formData: {},
      selectForm: {},
      communityOptions: [],
      buildingOptions: [],
      unitOptions: [],
      houseOptions: [],
      houseInfo: {},
      liveGridIdProps: {
        label: 'name',
        value: 'id',
        checkStrictly: true
      },
      dictProps: {
        label: 'name',
        value: 'code',
        checkStrictly: true
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: '请选择出生日期', trigger: 'blur' }
        ],
        identityNumber: [
          { validator: validateIdefer, required: true, trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        nation: [
          { required: true, message: '请选择民族', trigger: 'blur' }
        ],
        birthplace: [
          { required: true, message: '请输入籍贯', trigger: 'blur' }
        ],
        maritalStatus: [
          { required: true, message: '请选择婚姻状况', trigger: 'blur' }
        ],
        education: [
          { required: true, message: '请选择学历', trigger: 'blur' }
        ],
        householdAddr: [
          { required: true, message: '请输入户籍地', trigger: 'blur' }
        ],
        householdAddrDetail: [
          { required: true, message: '请输入户籍地详址', trigger: 'blur' }
        ],
        currentAddr: [
          { required: true, message: '请输入现住地', trigger: 'blur' }
        ],
        currentAddrDetail: [
          { required: true, message: '请输入现住地详址', trigger: 'blur' }
        ],
        liveGridIdArr: [
          { required: true, message: '请选择所属网格', trigger: 'blur' }
        ]
      },
      genderOption: [],
      nationOption: [],
      maritalStatusOption: [],
      politicalStatusOption: [],
      educationOption: [],
      religionOption: [],
      careerCategoryOption: [],
      correctionOption: [],
      caseTypeOption: [],
      acceptTypeOption: [],
      fourHistoryOption: [],
      threeRelatOption: [],
      correctionTeamOption: [],
      releaseOption: [],
      placeType: '小区',
      isYardSelectShow: false,
      isCommunitySelectShow: true,
      yardOptions: [],
      isReadOnly: false,
      disabled: false
    }
  },
  components: {
    AppTree,
    BtnGroup,
    AppTable
  },
  mounted () {
    if (this.genderOption.length === 0) {
      this.genderOption = getDictDataOption('性别')
    }
    this.correctionOption = getDictDataOption('矫正类别')
    this.getTreeData()
    this.getTableData()
  },
  methods: {
    getCourtyardList (param) {
      let m = this
      findCourtyardList(param).then(resp => {
        if (resp.code && resp.code === '200') {
          let data = resp.data
          if (data && data instanceof Array) {
            m.yardOptions = data
          }
        } else {
          return ''
        }
      })
    },
    placeTypeChangedEvt (val) {
      if (val) {
        let m = this
        const getPlaceType = {
          '小区': function () {
            m.isYardSelectShow = false
            m.isCommunitySelectShow = true
          },
          '院落': function () {
            if (m.yardOptions.length === 0 && m.formData.gridId && m.formData.gridId !== '') {
              m.getCourtyardList({likeAllGridId: m.formData.gridId})
            }
            m.isYardSelectShow = true
            m.isCommunitySelectShow = false
          }
        }
        getPlaceType[val]()
      }
    },
    identityNumberHaveDone (value) {
      if (!value) {
        return ''
      } else {
        getPopulation(value).then(resp => {
          if (resp.code && resp.code === '200') {
            let id = this.formData.id ? this.formData.id : null
            for (let key in resp.data) {
              if (key === 'id') continue
              this.$set(this.formData, key, resp.data[key])
            }
            let maritalStatusArr = this.formData.maritalStatusArr = this.formData.maritalStatus !== '' && this.formData.maritalStatus !== null ? this.formData.maritalStatus.split(',') : []
            let educationArr = this.formData.educationArr = this.formData.education !== '' && this.formData.education !== null ? this.formData.education.split(',') : []
            this.$set(this.formData, 'maritalStatusArr', maritalStatusArr)
            this.$set(this.formData, 'educationArr', educationArr)
            if (id !== null) this.$set(this.formData, 'id', id)
            if (this.formData.liveHouseId) {
              let liveHouseId = this.formData.liveHouseId
              let m = this
              getHouse(liveHouseId).then(resp => {
                if (resp.code && resp.code === '200') {
                  m.afterCommunityIdCheck(resp.data.communityId)
                  m.afterBuildIdCheck(resp.data.buildingId)
                  m.afterUnitIdCheck(resp.data.unitId)
                  m.afterGridIdCheck(resp.data.gridId)
                  m.$set(m.formData, 'unitId', resp.data.unitId)
                  m.$set(m.formData, 'buildingId', resp.data.buildingId)
                  m.$set(m.formData, 'communityId', resp.data.communityId)
                }
              })
            }
            let regionData = {}
            regionData.id = this.formData.liveGridId
            regionData.items = this.treeData
            let liveGridIdArr = getAllRegion(regionData)
            this.$set(this.formData, 'liveGridIdArr', liveGridIdArr)
            this.$set(this.formData, 'houseId', this.formData.liveHouseId)
          } else {
            return false
          }
        })
      }
    },
    // 格式化矫正类型
    correctionCategoryFormatter (row, column) {
      let correctionCategoryOption = getDictDataOption('矫正类别')
      let resCorrectionCategory = ''
      for (let i = 0; i < correctionCategoryOption.length; i++) {
        if (correctionCategoryOption[i].code === row.correctionCategory) {
          resCorrectionCategory = correctionCategoryOption[i].name
          break
        }
      }
      return resCorrectionCategory
    },
    afterGridIdCheck (gridId) {
      if (gridId && gridId !== '') {
        this.getCourtyardList({likeAllGridId: gridId})
        findCommunityList({likeAllGridId: gridId}).then(resp => {
          if (resp.code && resp.code === '200') {
            this.communityOptions = resp.data
          } else {
            return ''
          }
        })
      } else {
        if (this.formData.liveGridIdArr && this.formData.liveGridIdArr.length > 0) {
          let len = this.formData.liveGridIdArr.length - 1
          let param = {
            likeAllGridId: this.formData.liveGridIdArr[len]
          }
          this.getCourtyardList(param)
          findCommunityList(param).then(resp => {
            if (resp.code && resp.code === '200') {
              this.communityOptions = resp.data
            } else {
              return ''
            }
          })
        } else {
          this.$set(this.formData, 'courtyardId', '')
          this.$set(this.formData, 'communityId', '')
          this.$set(this.formData, 'buildingId', '')
          this.$set(this.formData, 'unitId', '')
          this.$set(this.formData, 'houseId', '')
        }
      }
    },
    afterCommunityIdCheck (val) {
      if (val) {
        let param = {communityId: val}
        findBuildingList(param).then(resp => {
          if (resp.code && resp.code === '200') {
            this.buildingOptions = resp.data
          } else {
            return ''
          }
        })
      }
    },
    afterBuildIdCheck (val) {
      if (val) {
        let param = {buildingId: val}
        findUnitList(param).then(resp => {
          if (resp.code && resp.code === '200') {
            this.unitOptions = resp.data
          } else {
            return ''
          }
        })
      }
    },
    afterUnitIdCheck (val) {
      if (val) {
        let param = {unitId: val}
        findHouseList(param).then(resp => {
          if (resp.code && resp.code === '200') {
            this.houseOptions = resp.data
          } else {
            return ''
          }
        })
      }
    },
    getTreeData () {
      let data = getUserGridTree()
      if (data && data instanceof Array) {
        this.treeData = data
      }
    },
    save (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          this.$alert('请填写正确表单格式', '', {
            confirmButtonText: '确定'
          })
          return ''
        } else {
          if (this.formData.liveGridIdArr && this.formData.liveGridIdArr !== 0) {
            let index = this.formData.liveGridIdArr.length - 1
            let data = {}
            data.id = this.formData.liveGridIdArr[index]
            data.items = this.treeData
            data.isGetAllId = true
            this.formData.allGridId = getAllRegion(data)
          } else {
            this.formData.allGridId = ''
          }
          if (this.formData.fourHistoryArr) {
            this.formData.fourHistory = this.formData.fourHistoryArr.join(',')
          }
          if (this.formData.threeRelatedArr) {
            this.formData.threeRelated = this.formData.threeRelatedArr.join(',')
          }
          if (this.formData.correctionTeamArr) {
            this.formData.correctionTeamSituation = this.formData.correctionTeamArr.join(',')
          }
          this.formData.maritalStatus = this.formData.maritalStatusArr ? this.formData.maritalStatusArr.join(',') : ''
          this.formData.education = this.formData.educationArr ? this.formData.educationArr.join(',') : ''
          if (this.formData.id) {
            updateCorrector(this.formData).then(resp => {
              if (resp.code && resp.code === '200') {
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
                this.getTableData()
              }
            })
          } else {
            createCorrector(this.formData).then(resp => {
              if (resp.code && resp.code === '200') {
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.getTableData()
              }
            })
          }
        }
      })
    },
    del (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.forEach(row => {
            delCorrector(row).then(resp => {
              if (resp.code && resp.code === '200') {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this.getTableData()
              }
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    cancel () {
      this.dialogFormVisible = false
      this.formData = {}
    },
    getTableData (param) {
      findCorrector(param).then(resp => {
        if (resp.code && resp.code === '200') {
          this.pageData = resp.data
          if (this.pageData.items && this.pageData.items instanceof Array) {
            this.tableData = this.pageData.items
          }
        }
      })
    },
    handleCurrentChange (val) {
      this.selectForm.offset = (val - 1) * 10
      this.getTableData(this.selectForm)
    },
    btnClickCallback (callbackData) {
      if (callbackData.btnType !== 'post' && !this.specialPassword) {
        this.$prompt('请输入授权码', '授权码', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^[a-zA-Z0-9_]{0,}$/,
          inputErrorMessage: '授权码格式不正确',
          closeOnClickModal: false,
          inputType: 'password'
        }).then(({ value }) => {
          this.getDictOption()
          let userInfo = getUserInfo()
          if (value !== userInfo.specialPassword || !userInfo.specialPassword || userInfo.specialPassword === '') {
            this.$message.error('授权码不正确')
            return ''
          }
          if (callbackData.btnType === 'delete') {
            this.dialogFormVisible = false
            let rows = callbackData.rows
            this.del(rows)
          } else {
            this.isfooterBtnShow = callbackData.btnType !== 'get'
            this.getCallBackInfo(callbackData)
            this.specialPassword = true
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      } else {
        this.isfooterBtnShow = true
        this.getDictOption()
        this.dialogTitle = '新增'
        if (this.formData.liveGridIdArr) {
          let arr = this.formData.liveGridIdArr
          this.formData = {}
          this.$set(this.formData, 'liveGridIdArr', arr)
        } else {
          this.formData = {}
        }
        this.showOnly = callbackData.sohwOnly
        this.dialogTitle = callbackData.dialogTitle
        this.dialogFormVisible = true
      }
    },
    getDictOption () {
      this.nationOption = getDictDataOption('民族')
      this.maritalStatusOption = getDictDataOption('婚姻状况')
      this.politicalStatusOption = getDictDataOption('政治面貌')
      this.educationOption = getDictDataOption('学历')
      this.religionOption = getDictDataOption('宗教信仰')
      this.careerCategoryOption = getDictDataOption('职业分类')
      this.caseTypeOption = getDictDataOption('案件类别')
      this.acceptTypeOption = getDictDataOption('接收方式')
      this.fourHistoryOption = getDictDataOption('四史情况')
      this.threeRelatOption = getDictDataOption('三涉情况')
      this.correctionTeamOption = getDictDataOption('矫正小组人员组成类别')
      this.releaseOption = getDictDataOption('矫正解除（终止）情况')
    },
    getCallBackInfo (callbackData) {
      let _this = this
      getCorrector(callbackData.rows[0].id).then(resp => {
        if (resp.code && resp.code === '200') {
          let obj = JSON.stringify(resp.data)
          _this.formData = JSON.parse(obj)
          _this.isYardSelectShow = !!_this.formData.courtyardId && _this.formData.courtyardId !== ''
          _this.isCommunitySelectShow = !_this.isYardSelectShow
          if (_this.formData.houseId) {
            let houseId = _this.formData.houseId
            getHouse(houseId).then(resp => {
              if (resp.code && resp.code === '200') {
                _this.afterCommunityIdCheck(resp.data.communityId)
                _this.afterBuildIdCheck(resp.data.buildingId)
                _this.afterUnitIdCheck(resp.data.unitId)
                _this.afterGridIdCheck('', resp.data.gridId)
                _this.$set(_this.formData, 'unitId', resp.data.unitId)
                _this.$set(_this.formData, 'buildingId', resp.data.buildingId)
                _this.$set(_this.formData, 'communityId', resp.data.communityId)
              }
            })
          }
          let maritalStatusArr = _this.formData.maritalStatus !== '' && _this.formData.maritalStatus !== null ? _this.formData.maritalStatus.split(',') : []
          let educationArr = _this.formData.education !== '' && _this.formData.education !== null ? _this.formData.education.split(',') : []
          let fourHistoryArr = _this.formData.fourHistory !== '' && _this.formData.fourHistory !== null ? _this.formData.fourHistory.split(',') : []
          let threeRelatedArr = _this.formData.threeRelated !== '' && _this.formData.threeRelated !== null ? _this.formData.threeRelated.split(',') : []
          let correctionTeamArr = _this.formData.correctionTeamSituation !== '' && _this.formData.correctionTeamSituation !== null ? _this.formData.correctionTeamSituation.split(',') : []
          _this.$set(_this.formData, 'maritalStatusArr', maritalStatusArr)
          _this.$set(_this.formData, 'educationArr', educationArr)
          _this.$set(_this.formData, 'fourHistoryArr', fourHistoryArr)
          _this.$set(_this.formData, 'threeRelatedArr', threeRelatedArr)
          _this.$set(_this.formData, 'correctionTeamArr', correctionTeamArr)
          let liveRegionData = {}
          liveRegionData.id = _this.formData.gridId
          liveRegionData.items = _this.treeData
          let liveGridIdArr = getAllRegion(liveRegionData)
          _this.$set(_this.formData, 'liveGridIdArr', liveGridIdArr)
          _this.showOnly = callbackData.sohwOnly
          _this.dialogTitle = callbackData.dialogTitle
          _this.dialogFormVisible = true
          _this.isReadOnly = callbackData.btnType === 'put'
          _this.disabled = callbackData.btnType === 'put'
        }
      })
    },
    handleCheckedRows (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.currentSelectedData = rows
      } else {
        this.currentSelectedData = []
      }
    },
    appTreeClick (data) {
      let gridData = {}
      gridData.id = data.id
      gridData.items = this.treeData
      let liveGridIdArr = getAllRegion(gridData)
      this.$set(this.formData, 'liveGridIdArr', liveGridIdArr)
      this.afterGridIdCheck(data.id)
      this.selectForm.likeAllGridId = data.id
      this.selectForm.offset = 0
      this.getTableData(this.selectForm)
    },
    searchHandleClick () {
      this.selectForm.offset = 0
      this.getTableData(this.selectForm)
    }
  }
}
</script>
<style>
</style>
